package jh.com.jhappbf.httputils;

import android.os.Build;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.RequiresApi;
import jh.com.jhappbf.configs.Constant;

/**
 * @author Administrator
 */
public class VolleyCallWebService extends StringRequest {

    private byte[] mRequestData;
    private String mSoapAction;
    private Priority mPriority;

    public VolleyCallWebService(String methodName, ArrayList<String> param, ArrayList<String> value,
                                Listener<String> listener, ErrorListener errorListener, Priority priority) {
        super(Method.POST, Constant.WEBSERVICE, listener, errorListener);
        mSoapAction = "http://tempuri.org/" + methodName;
        mRequestData = getRequestData(methodName, param, value);
        mPriority = priority;
    }

    @Override
    public com.android.volley.Request.Priority getPriority() {
        return mPriority;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> map = new HashMap<>();
        map.put("Content-Length", String.valueOf(mRequestData.length));
        map.put("SOAPAction", mSoapAction);
        return map;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        return mRequestData;
    }

    @Override
    public String getBodyContentType() {
        return "text/xml; charset=utf-8";
    }

    /**
     * @param methodName web接口的函数名称
     * @param parameters web接口的参数名字
     * @param parValues  web接口的参数值
     * @return 返回soap格式的请求数据
     *
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private byte[] getRequestData(String methodName,
                                  ArrayList<String> parameters, ArrayList<String> parValues) {
        Log.d("1234222", "commitUnloads: "+methodName);
        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                + "<soap:Body />";
        String tps, vps, ts;
        StringBuilder merakString;
        merakString = new StringBuilder("<" + methodName + " xmlns=\"http://tempuri.org/\">");
        for (int i = 0; i < parameters.size(); i++) {
            tps = parameters.get(i);
            // 设置该方法的参数为.net webService中的参数名称
            vps = parValues.get(i);
            ts = "<" + tps + ">" + vps + "</" + tps + ">";
            merakString.append(ts);
        }
        merakString.append("</").append(methodName).append(">");
        String soap2 = "</soap:Envelope>";
        String requestData = soap + merakString + soap2;
        byte[] bytes;
        bytes = requestData.getBytes(StandardCharsets.UTF_8);

        return bytes;
    }
}
